package com.jy.servlet;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.jy.dao.UserDao;
import com.jy.dao.impl.UserDaoImpl;
import com.jy.entity.*;
import com.jy.util.TurnInt;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.*;

@WebServlet("/TestSubmitServlet/*")
public class TestSubmitServlet extends BaseServlet {
    UserDaoImpl user=new UserDaoImpl();
    /**
     * 对的答案
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void truefalseansw(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ParseException {
        response.setContentType("text/html;charset=UTF-8");
        //作答结果
        List<String> jg = new ArrayList<String>();
        //单选用户的答案
        String hj=request.getParameter("ra");
        //多选用户的答案
        String hj2=request.getParameter("ch");
        //得到用户id
        int uid= (int) request.getSession().getAttribute("stuid");

        Student stu=user.getStudent(uid);
        String sname=stu.getStname();

        //得到考试ID
        String exid=request.getParameter("ksid");
        //得到考试时间
        String tiem=request.getParameter("zttime");
        //考试名称
        String examname="";
        //考试类型
        String examtype="";
        //得到考试类型 考试名称
        int exaid=Integer.parseInt(exid);
        List ulis=user.seexamconfig(exaid);
        for(int i=0;i<ulis.size();i++){
            ExamRecord ex=(ExamRecord)ulis.get(i);
            examname=ex.getExam_name();
            examtype=ex.getExam_type();
        }
        //总分
        int Total=0;
        List<Integer> falselist = new ArrayList<Integer>();
        int ForThe=0;//对的
        int Wrong=0;//错数
        //数据库取出的答案
        Map<Integer, String> map = new HashMap<Integer, String>();
        List lis=user.seanswer(exaid);
        //保存题目ID 和 答案
        for(int i=0;i<lis.size();i++){
            Question qu=(Question)lis.get(i);
            map.put(qu.getQuestion_id(),qu.getQuestion_answer());
        }
        if(!("".equals(hj))){
            //单选
            JSONArray array=JSONArray.fromObject(hj);
            for(int i=0;i<array.size();i++){
                JSONObject tempDept =array.getJSONObject(i);
                int id=tempDept.getInt("tid");
                String val=tempDept.getString("val");
                int sc=tempDept.getInt("score");
                System.out.println("单选:"+id);
                System.out.println("用户答案:"+val);
                System.out.println("分数:"+sc);
                /*System.out.print(id);*/
                if(val.equals(map.get(id))){
                    Total+=sc;
                    ForThe++;
                    Question q1=user.getquestion("select *from question where question_id=?",id);
                    DetailPage dp=new DetailPage();
                    dp.setQuestion_id(id);
                    dp.setUser_id(uid);
                    dp.setExam_id(exaid);
                    dp.setUser_answer(val);
                    dp.setQuestion_name(q1.getQuestion_name());
                    dp.setQuestion_answera(q1.getQuestion_optiona());
                    dp.setQuestion_answerb(q1.getQuestion_optionb());
                    dp.setQuestion_answerc(q1.getQuestion_optionc());
                    dp.setQuestion_answerd(q1.getQuestion_optiond());
                    dp.setQuestion_answer(q1.getQuestion_answer());
                    dp.setExam_type("单选");
                    dp.setQuestion_score(q1.getQuestion_score());
                    System.out.println("试卷解析:"+dp);
                    int n=user.adddetailpage(dp);
                    if(n>0){
                        System.out.println("detailpage添加成功");
                    }
                }else{
                    Wrong++;
                    falselist.add(id);
                    Question q1=user.getquestion("select *from question where question_id=?",id);
                    FalseBook fb=new FalseBook();
                    fb.setQuestion_id(id);
                    fb.setUser_id(uid);
                    fb.setExam_id(exaid);
                    fb.setUser_answer(val);
                    fb.setQuestion_answera(q1.getQuestion_optiona());
                    fb.setQuestion_answerb(q1.getQuestion_optionb());
                    fb.setQuestion_answerc(q1.getQuestion_optionc());
                    fb.setQuestion_answerd(q1.getQuestion_optiond());
                    fb.setQuestion_answer(q1.getQuestion_answer());
                    fb.setExam_type("单选");
                    fb.setQuestion_name(q1.getQuestion_name());
                    System.out.println("错题(单选):"+fb);

                    int tj=user.addfalsebook(fb);
                    if(tj>0){
                        System.out.println("add成功");
                    }
                    DetailPage dp=new DetailPage();
                    dp.setQuestion_id(id);
                    dp.setQuestion_name(q1.getQuestion_name());
                    dp.setUser_id(uid);
                    dp.setExam_id(exaid);
                    dp.setUser_answer(val);
                    dp.setQuestion_answera(q1.getQuestion_optiona());
                    dp.setQuestion_answerb(q1.getQuestion_optionb());
                    dp.setQuestion_answerc(q1.getQuestion_optionc());
                    dp.setQuestion_answerd(q1.getQuestion_optiond());
                    dp.setQuestion_answer(q1.getQuestion_answer());
                    dp.setExam_type("单选");
                    dp.setQuestion_score(q1.getQuestion_score());
                    System.out.println("试卷解析:"+dp);
                    int n=user.adddetailpage(dp);
                    if(n>0){
                        System.out.println("detailpage添加成功");
                    }
                }

                jg.add(id+"="+val+",");
             /*   response.getWriter().write((i+1)+"用户答案："+val);
                response.getWriter().write(" 正确答案："+map.get(id));
                response.getWriter().write("<br />");*/
                System.out.println("正确答案:"+map.get(id));
            }

            /*response.getWriter().write("得分："+Total);
            response.getWriter().write("<hr>");*/
        }
        if(!("".equals(hj2))){
            //多选
            JSONArray array2=JSONArray.fromObject(hj2);
            for(int i=0;i<array2.size();i++){
                JSONObject tempDept2 =array2.getJSONObject(i);
                int id=tempDept2.getInt("tid");
                int sc=tempDept2.getInt("score");
                System.out.println("id:"+id);
                String dxval = map.get(id);
                System.out.println("dxval："+dxval);
                JSONObject line=JSONObject.fromObject(array2.get(i));
                JSONArray carList=JSONArray.fromObject(line.getString("val"));
                String dval = "";
                for(int j=0;j<carList.size();j++){
                    dval+=carList.get(j);
                }
                int ts = TurnInt.rurnit(dxval);
                int ts2 = TurnInt.rurnit(dval);
                if(ts==ts2){
                    Question q1=user.getquestion("select *from question where question_id=?",id);
                    DetailPage dp=new DetailPage();
                    dp.setQuestion_id(id);
                    dp.setUser_id(uid);
                    dp.setExam_id(exaid);
                    dp.setUser_answer(dval);
                    dp.setQuestion_name(q1.getQuestion_name());
                    dp.setQuestion_answera(q1.getQuestion_optiona());
                    dp.setQuestion_answerb(q1.getQuestion_optionb());
                    dp.setQuestion_answerc(q1.getQuestion_optionc());
                    dp.setQuestion_answerd(q1.getQuestion_optiond());
                    dp.setQuestion_answer(q1.getQuestion_answer());
                    dp.setExam_type("多选");
                    dp.setQuestion_score(q1.getQuestion_score());
                    System.out.println("试卷解析:"+dp);
                    int n=user.adddetailpage(dp);
                    if(n>0){
                        System.out.println("detailpage添加成功");
                    }
                    Total+=sc;
                    ForThe++;
                }else{
                    Wrong++;
                    falselist.add(id);
                    Question q1=user.getquestion("select *from question where question_id=?",id);
                    FalseBook fb=new FalseBook();
                    fb.setQuestion_id(id);
                    fb.setUser_id(uid);
                    fb.setExam_id(exaid);
                    fb.setUser_answer(dval);
                    fb.setQuestion_answera(q1.getQuestion_optiona());
                    fb.setQuestion_answerb(q1.getQuestion_optionb());
                    fb.setQuestion_answerc(q1.getQuestion_optionc());
                    fb.setQuestion_answerd(q1.getQuestion_optiond());
                    fb.setQuestion_answer(q1.getQuestion_answer());
                    fb.setExam_type("多选");
                    fb.setQuestion_name(q1.getQuestion_name());
                    System.out.println("错题(多选):"+fb);
                    int tj=user.addfalsebook(fb);
                    if(tj>0){
                        System.out.println("add成功");
                    }
                    DetailPage dp=new DetailPage();
                    dp.setQuestion_id(id);
                    dp.setUser_id(uid);
                    dp.setExam_id(exaid);
                    dp.setUser_answer(dval);
                    dp.setQuestion_name(q1.getQuestion_name());
                    dp.setQuestion_answera(q1.getQuestion_optiona());
                    dp.setQuestion_answerb(q1.getQuestion_optionb());
                    dp.setQuestion_answerc(q1.getQuestion_optionc());
                    dp.setQuestion_answerd(q1.getQuestion_optiond());
                    dp.setQuestion_answer(q1.getQuestion_answer());
                    dp.setExam_type("多选");
                    dp.setQuestion_score(q1.getQuestion_score());
                    int n=user.adddetailpage(dp);
                    if(n>0){
                        System.out.println("detailpage添加成功");
                    }
                    System.out.println("试卷解析:"+dp);
                }
                jg.add(id+"="+dval+",");
                /*response.getWriter().write((i+1)+"用户答案："+dval);
                response.getWriter().write(" 正确答案："+map.get(id));
                response.getWriter().write("<br />");*/
            }
        }
        System.out.println("做错了的题目id:"+falselist.toString());
        String pjg="";
        if(jg.size()!=0){
            String tt="";
            for(String t:jg){
                tt+=t;
            }
            pjg=tt.substring(0,tt.length()-1);
        }
        String kscj=String.valueOf(Total);
        Date d=new Date();
        ExamRecord exa=new ExamRecord();
        exa.setUser_id(uid);
        exa.setExam_id(exaid);
        exa.setExam_score(kscj);
        exa.setExam_results(pjg);
        exa.setExam_time(d);
        exa.setExam_zt("已交卷");
       System.out.println("exa:="+exa);
        int cf=user.updaterecord(exa);
        if(cf>0) {
            System.out.println("成绩：" + kscj);
            request.getSession().setAttribute("score", kscj);
            request.getSession().setAttribute("dui", ForThe);
            request.getSession().setAttribute("Wrong", Wrong);
            request.getSession().setAttribute("ksmc", examname);
            request.getSession().setAttribute("sname", sname);
            PrintWriter out = response.getWriter();
            out.print("<script>localStorage.clear();location.href='/examproject/html/Score.jsp'</script>");
        }
    }

}
